package com.xb.p7_popmenu;

import android.os.Bundle;

import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.google.android.material.snackbar.Snackbar;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.PopupMenu;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity implements View.OnClickListener, PopupMenu.OnMenuItemClickListener {

    Button button;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });

        button=findViewById(R.id.pop_menu);
        button.setOnClickListener(this);
    }

    //弹出菜单
    @Override
    public void onClick(View v) {
        PopupMenu popupMenu =new PopupMenu(this,v);

        //将自定义的菜单填充过来
        popupMenu.inflate(R.menu.menu_main);

        //菜单项绑定到点击事件
        popupMenu.setOnMenuItemClickListener(this);

        popupMenu.show();
    }

    //弹出式菜单项的点击事件
    @Override
    public boolean onMenuItemClick(MenuItem item) {
        switch (item.getItemId()){
            case R.id.action_copy:
                Toast.makeText(this,"复制了内容",Toast.LENGTH_SHORT).show();
                break;
            case R.id.action_paste:
                Toast.makeText(this,"粘贴了内容",Toast.LENGTH_SHORT).show();
                break;
        }
        return false;
    }
}
